import SceneBase from "./SceneBase";
import TextureMgr from "../../engine/uicomponent/TextureMgr";
import RandomUtil from "../../engine/utils/RandomUtil";
import FishMover from "../../../fish/script/FishMover";
import { FishPathInfo } from "../config/FishPathInfo";
import { FishPathConfig } from "../config/FishPathConfig";
import { Logger } from "../../engine/utils/Logger";
import FishWiki from "../../../fish/script/FishWiki";
import GameMusicHelper from "../utils/GameMusicHelper";
import BundleManager from "../../../../base/Script/core/BundleManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class FishGameScene extends SceneBase {

    @property(cc.Sprite)
    private bg: cc.Sprite = null;

    @property({ type: [cc.Prefab] })
    private fishPrefabList: Array<cc.Prefab> = [];

    private showNode: cc.Node = null;

    onLoadMe() {
        GameMusicHelper.playBg();
        FishPathConfig.init();
        this.initBg();
        // this.testPathPlay()
    }

    private initBg() {
        let textureMgr: TextureMgr = this.bg.getComponent(TextureMgr);
        this.bg.spriteFrame = textureMgr.Spriteset[RandomUtil.nextInt(0, textureMgr.Spriteset.length - 1)]
    }

    private initShowNode() {
        if (this.showNode) {
            this.showNode.destroy();
            this.showNode = null;
        }
        let fishType: number = 29
        if (fishType < 1 || fishType > 29) {
            return;
        }
        this.showNode = cc.instantiate(this.fishPrefabList[fishType - 1])
        this.showNode.getComponent(FishMover).speed = 2
        this.showNode.getComponent(FishMover).node.setScale(2);
        this.node.addChild(this.showNode)
    }

    private testPathPlay() {
        this.initShowNode()
        let pathInfo: FishPathInfo = FishPathConfig.getPathInfo(3);
        Logger.log("testPathPlay=pathInfo=", pathInfo)
        let params = pathInfo.path;
        Logger.log( "testPathPlay=11=", params[0])
        this.showNode.setPosition(params[0])
        this.showNode.getComponent(FishMover).bezierPList = params;
        this.showNode.getComponent(FishMover).startMove();
    }

    private onClickWiki(){
        FishWiki.show()
    }

    onDestroyMe() {
        this.unscheduleAllCallbacks();
        this.node.stopAllActions();
        BundleManager.ins().releaseAll('FishSingle');
    }

}
